// packageStore/pages/distribution/main/main.js
import {
  api
} from '../../../js/util';
import BasePage from '../../../js/common/base-page';
import {
  pagesInfo,
  router
} from '../../../js/router';
import {
  distribution
} from "../../../js/modules/distribution.js";
import * as echarts from '../../components/ec-canvas/echarts';
const app = getApp();
BasePage({
  /**
   * 页面的初始数据
   */
  data: {
    headerBg: "",
    staticsListOrigin: [{
        name: '累计佣金',
        money: '0'
      },
      {
        name: '待结算佣金',
        money: '0'
      },
      {
        name: '客户总数',
        money: '0'
      },
      {
        name: '本月新增客户',
        money: '0'
      },
      {
        name: '本月佣金',
        money: '0'
      },
    ],
    mode:'cash',
    withdrawableAmount: 0,
    staticsList: [],
    funcList: [{
        link: "/" + pagesInfo.distributionSub.url,
        icon: 'icon-customer',
        name: '下级客户',
        show: true 
      },
      {
        link: "/" + pagesInfo.distributionOrderList.url,
        icon: 'icon-orders',
        name: '分销订单',
        show: true 
      },
      {
        link: "/" + pagesInfo.distributionCommission.url,
        icon: 'icon-commision',
        name: '分销佣金',
        show: true 
      },
      {
        link: "/" + pagesInfo.distributionRank.url,
        icon: 'icon-rank',
        name: '我的排名',
        show: true 
      },
      {
        link: "/" + pagesInfo.index.url+'?indexShowDialog=true',
        icon: 'icon-share-mall',
        name: '分享商城',
        show: true 
      },
      {
        link: "/" + pagesInfo.productList.url,
        icon: 'icon-share-goods',
        name: '分享商品',
        show: true 
      },
      {
        link: "/" + pagesInfo.promotionlist.url,
        icon: 'icon-share-promote',
        name: '分享活动',
        show: true 
      },
      {
        link: "/" + pagesInfo.distributionCard.url,
        icon: 'icon-share-card',
        name: '分享名片',
        show: true 
      }
    ],
    ec: {
      lazyLoad:true
    },
    echartNames:['01-09','01-24','01-29','02-03','02-08','02-13'],
    echartValues:[0, 1, 2, 5, 4, 2, 3],
    Currency:'',//货币符号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.getDataStatics();
    this.echartsComponnet = this.selectComponent('#mychart');
    this.reloadStatics();
  },
  /**
   * 统计数据重构
   */
  reloadStatics:function(){
    let result = [];
    let array = this.data.staticsListOrigin,
      len = array.length;
    for (let i = 0; i < (len - 1); i += 2) {
      if (i == 2) {
        result.push(array.slice(i, len));
      } else {
        result.push(array.slice(i, i + 2));
      }
    }
    this.setData({
      staticsList: result
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      headerBg: api.titleColor || "#e56b14",
      Currency:api.Currency || '￥'
    })
  },
  /**
   * 获取统计数据
   */
  getDataStatics: function () {
    var that = this
    distribution.dataStatistics(function (content) {
      if (content != null) {
        var staticsListArry = that.data.staticsListOrigin;
        for(let i=0;i<staticsListArry.length;i++){
          switch(staticsListArry[i].name){
            case "累计佣金":
              staticsListArry[i].money = content.CumulativeCommission;
              break;
            case "待结算佣金":
              staticsListArry[i].money = content.UnSettlementCommission;
              break;
            case "客户总数":
              staticsListArry[i].money = content.AllDistributor;
              break;
            case "本月新增客户":
              staticsListArry[i].money = content.MonthDistributor;
              break;
            default:
              staticsListArry[i].money = content.MonthCommission;
          }
        }
        that.setData({
          withdrawableAmount: content.WithdrawableAmount,
          staticsListOrigin:staticsListArry,
          mode: content.DistributionCommissionMode
        },function(){
          that.reloadStatics();
        })
      }
    })
  },
  /**
   * 获取设备像素比
   */
  getPixelRatio:function(){
    let pixelRatio = 0;
    wx.getSystemInfo({
      success:function(res){
        pixelRatio = res.pixelRatio
      },
      fail:function(){
        pixelRatio = 0;
      }
    })
    return pixelRatio;
  },
  /**
   * 初始化图表
   */
  init_echarts:function(){
    var dpr = this.getPixelRatio();
    this.echartsComponnet.init((canvas, width, height) => {
      // 初始化图表
      const Chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr
      });
      Chart.setOption(this.getOption());
      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return Chart;
    });
  },
  /**
   * 动态化图表数据
   */
  getOption:function(){
    let that = this;
    var option = {
      legend:{
        data:['新增客户数']
      },
      tooltip: {
        trigger: 'axis',
        formatter:'{b}{a}：{c}'
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: that.data.echartNames,
        splitLine: {
            lineStyle: {
            color: '#f8f8f8'
            },
            show: true
        },
        axisLabel: {
            show: true,
            textStyle: {
            color: '#999'
            }
        },
        axisLine: {
            lineStyle: {
            color: '#039d12'
            }
        }
      },
      yAxis: {
          type: 'value',
          data:[0,1,2,3,4,5],
          splitLine: {
              lineStyle: {
              color: '#f8f8f8'
              },
              show: true
          },
          axisLabel: {
              show: true,
              textStyle: {
              color: '#999'
              }
          },
          axisLine: {
              lineStyle: {
              color: '#f8f8f8'
              }
          }
      },
      series: [
          {
              data: that.data.echartValues,
              name:'新增客户数',
              type: 'line',
              itemStyle: {
                  normal: {
                      color: '#039d12',
                      lineStyle: {
                          color: '#039d12',
                          opacity: 0.8
                      }
                  }
              },
              areaStyle: {
                  normal: {
                      color: 'rgba(151, 147, 221, 0.1)'
                  }
              }
          }
      ]
    };
    return option;
  },
  /**
   * 提现
   */
  goCashOut: function () {
    router.navigate({
      path: 'distributionCashOut',
      params: {
        type: 1 //分销
      }
    })
  },
  /**
   * 日期选择
   */
  dateSelect:function(e){
    let that = this;
    let dateType;
    if(e.detail.mode=='月'){
      dateType = 2;
    }else if(e.detail.mode=='季'){
      dateType = 3;
    }else if(e.detail.mode=='年'){
      dateType = 4;
    }
    let args = {
      dateType:dateType-1,
      dateTime:e.detail.value
    }
    that.getEchatsData(args);
  },
  /**
   * 获取新增客户数数据
   */
  getEchatsData:function(args){
    let that = this;
    distribution.getAddCustomerStatistics(args,function(content){
      let names = [],values=[];
      if(content && content.length>0){
        for(let i = 0;i<content.length;i++){
          let item = content[i];
          names.push(item.AuditTime.substring(5,item.AuditTime.length));
          values.push(item.DistributorCount||0);
        }
      }
      that.setData({
        echartNames:names,
        echartValues:values
      },function(){
        that.init_echarts();
      })
    })
  },
  onClickItem(e){
    wx.navigateTo({
      url: e.detail,
    })
  }
})